package controllers

import (
	"WORKSPACE/models"
	"WORKSPACE/pkg/app"
	"WORKSPACE/pkg/e"
	"WORKSPACE/services"
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"github.com/unknwon/com"
)

func GetAllwdata(c *gin.Context)  {
	err, info, total := services.GetAllwdata()
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{"value": info, "total": total},"查询成功")
}

func Delwdata(c *gin.Context){
	id := "-1"
	data := "-1"
	if arg := c.Query("Eno"); arg != "" {
		id = arg
		fmt.Println(id)
	}
	if id == "-1"{
		app.INFO(c,30001,"参数错误")
		return
	}
	if arg1 := c.Query("Wdata"); arg1 != "" {
		data = arg1
		fmt.Println(data)
	}
	if data == "-1"{
		app.INFO(c,30002,"参数错误")
		return
	}

	err := services.Delwdata(id,data)
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{},"删除成功")
}

func Updatewdata(c *gin.Context){
	data, _ := c.GetRawData()
	var m map[string]string
	_ = json.Unmarshal(data, &m)
	eno := m["Eno"]
	odata := m["Wdata"]
	wthing := com.StrTo(m["Wthing"]).MustInt()
	whouse := com.StrTo(m["Whouse"]).MustInt()
	wpost := com.StrTo(m["Wpost"]).MustInt()
	wbase := com.StrTo(m["Wbase"]).MustInt()
	wdepar := com.StrTo(m["Wdepar"]).MustInt()
	wprice := com.StrTo(m["Wprice"]).MustInt()
	coeff := com.StrTo(m["Coeff"]).MustInt()

	if m["Eno"] == ""{
		app.INFO(c, 30000, "参数非法")
		return
	}
	err := services.Updatewdata(models.Wdata{ Eno: eno, Wdata: odata, Wthing: wthing,Whouse:whouse,Wpost:wpost,Wbase:wbase,Wdepar:wdepar,Wprice:wprice,Coeff:coeff})
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{},"更新成功！")
}

func Addwdata(c *gin.Context){
	data, _ := c.GetRawData()
	var m map[string]string
	_ = json.Unmarshal(data, &m)
	eno := m["Eno"]
	odata := m["Wdata"]
	wthing := com.StrTo(m["Wthing"]).MustInt()
	whouse := com.StrTo(m["Whouse"]).MustInt()
	wpost := com.StrTo(m["Wpost"]).MustInt()
	wbase := com.StrTo(m["Wbase"]).MustInt()
	wdepar := com.StrTo(m["Wdepar"]).MustInt()
	wprice := com.StrTo(m["Wprice"]).MustInt()
	coeff := com.StrTo(m["Coeff"]).MustInt()

	if m["Eno"] == ""{
		app.INFO(c, 30000, "参数非法")
		return
	}
	err := services.Addwdata(models.Wdata{ Eno: eno, Wdata: odata, Wthing: wthing,Whouse:whouse,Wpost:wpost,Wbase:wbase,Wdepar:wdepar,Wprice:wprice,Coeff:coeff})
	fmt.Println(err)
	if err !=nil{
		app.Error(c,e.ERROR,err,err.Error())
		return
	}
	app.OK(c, map[string]interface{}{},"添加成功！")
}